package com.example.yangjianquan.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class SecondActivity extends AppCompatActivity {

    private TextView textView;
    private ListView listView;
    private List<Fruit> fruitList = new ArrayList<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        textView = (TextView)findViewById(R.id.infoTextView);

        Intent i = getIntent();
        textView.setText(i.getStringExtra("yang"));

        textView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("第二","第二的数据");
                setResult(RESULT_OK,intent);
                finish();
            }
        });


        listView = (ListView)findViewById(R.id.list_item);
        initFruits();
        FruitAdapter fruitAdapter = new FruitAdapter(SecondActivity.this,R.layout.listview_main,fruitList);
        listView.setAdapter(fruitAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitList.get(position);
                Toast.makeText(SecondActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initFruits() {
        Fruit apple = new Fruit("Apple", R.drawable.boluo);
        fruitList.add(apple);
        Fruit banana = new Fruit("Banana", R.drawable.boluo);
        fruitList.add(banana);
        Fruit orange = new Fruit("Orange", R.drawable.boluo);
        fruitList.add(orange);
        Fruit watermelon = new Fruit("Watermelon", R.drawable.boluo);
        fruitList.add(watermelon);
        Fruit pear = new Fruit("Pear", R.drawable.boluo);
        fruitList.add(pear);
        Fruit grape = new Fruit("Grape", R.drawable.boluo);
        fruitList.add(grape);
        Fruit pineapple = new Fruit("Pineapple", R.drawable.boluo);
        fruitList.add(pineapple);
        Fruit strawberry = new Fruit("Strawberry", R.drawable.boluo);
        fruitList.add(strawberry);
        Fruit cherry = new Fruit("Cherry", R.drawable.boluo);
        fruitList.add(cherry);
        Fruit mango = new Fruit("Mango", R.drawable.boluo);
        fruitList.add(mango);
    }
}
